#!/bin/sh

# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0

usage() {
  cat 1>&2 <<EOF
Installs the Smithy CLI

USAGE:
    install [FLAGS] [OPTIONS]

FLAGS:
    -u, --update              Updates the Smithy CLI if a different version
                              is previously installed. By default, this script
                              will not update the Smithy CLI if a previous
                              installation is detected.

    -h, --help                Prints help information

OPTIONS:
    -i, --install-dir <path>  The directory to install the Smithy CLI. By
                              default, this directory is: /usr/local/smithy

    -b, --bin-dir <path>      The directory to store symlinks to executables
                              for the Smithy CLI. By default, the directory
                              used is: /usr/local/bin
EOF
}

parse_commandline() {
  while test $# -gt 0
  do
    key="$1"
  case "$key" in
    -i|--install-dir)
      PARSED_INSTALL_DIR="$2"
      shift
      ;;
    -b|--bin-dir)
      PARSED_BIN_DIR="$2"
      shift
      ;;
    -u|--update)
      PARSED_UPGRADE="yes"
      ;;
    -h|--help)
      usage
      exit 0
      ;;
    *)
      quit "Got an unexpected argument: $1"
      ;;
    esac
      shift
  done
}

set_global_vars() {
  ROOT_INSTALL_DIR=${PARSED_INSTALL_DIR:-/usr/local/smithy}
  BIN_DIR=${PARSED_BIN_DIR:-/usr/local/bin}
  UPGRADE=${PARSED_UPGRADE:-no}

  EXE_NAME="smithy"
  INSTALLER_DIR="$( cd "$( dirname "$0" )" >/dev/null 2>&1 && pwd )"
  INSTALLER_EXE="$INSTALLER_DIR/bin/$EXE_NAME"
  SMITHY_VERSION=$($INSTALLER_EXE --version)

  INSTALL_DIR="$ROOT_INSTALL_DIR/$SMITHY_VERSION"

  CURRENT_INSTALL_DIR="$ROOT_INSTALL_DIR/current"
  CURRENT_SMITHY_EXE="$CURRENT_INSTALL_DIR/bin/$EXE_NAME"

  BIN_SMITHY_EXE="$BIN_DIR/$EXE_NAME"
}

create_install_dir() {
  echo "Installing Smithy CLI to '$INSTALL_DIR'..."
  mkdir -p "$INSTALL_DIR" || exit 1
  {
    cp -R "$INSTALLER_DIR/." "$INSTALL_DIR/." &&
    ln -snf "$INSTALL_DIR" "$CURRENT_INSTALL_DIR"
  } || {
    rm -rf "$INSTALL_DIR"
    exit 1
  }
}

check_preexisting_install() {
    echo "Checking for existing installations of Smithy CLI..."
  if [ -L "$CURRENT_INSTALL_DIR" ] && [ "$UPGRADE" = "no" ]
  then
    quit "Found preexisting Smithy CLI installation: $CURRENT_INSTALL_DIR. Please rerun install script with --update flag."
  fi
  if [ -d "$INSTALL_DIR" ]
  then
    echo "Found same Smithy CLI version: $INSTALL_DIR. Skipping install."
    exit 0
  fi
}

create_bin_symlinks() {
  echo "Setting up links..."
  mkdir -p "$BIN_DIR"
  ln -sf "$CURRENT_SMITHY_EXE" "$BIN_SMITHY_EXE"
}

quit() {
  err_msg="$1"
  echo "$err_msg" >&2
  exit 1
}

main() {
  parse_commandline "$@"
  set_global_vars
  check_preexisting_install
  create_install_dir
  create_bin_symlinks
  $BIN_SMITHY_EXE warmup
  echo "You can now run: '$BIN_SMITHY_EXE --version' or '$EXE_NAME --version'"
  case :$PATH: in
    *:$BIN_DIR:*)
    ;;
    *)
      echo "$BIN_DIR not detected in \$PATH, you will be unable to use '$EXE_NAME --version'"
    ;;
  esac
  exit 0
}

main "$@"
